package mc2014.hackerthon.sharingphonebook;

import mc2014.hackerthon.Adapters.MainPagerAdapter;
import mc2014.hackerthon.Dialog.GroupAddDialog;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.os.Build;

public class MainActivity extends FragmentActivity 
{

	private Button profile_btn;
	private SlidingMenu mainMenu;
	private boolean isclicked = false;
	private LinearLayout pageMark;
	private int prevPosition;
	private int numOfGrid;
	private Button addBtn;
	private int numOfGroup;
	ViewPager mainPager;
	private GroupAddDialog groupAddDialog;
	private Context context;
	private MainPagerAdapter mainAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		context = getApplicationContext();
		
		ActionBar actionbar = getActionBar();
		
		View actionbarCustomLayout = getLayoutInflater().inflate(R.layout.actionbar_custom_layout,null);
		
		profile_btn = (Button)actionbarCustomLayout.findViewById(R.id.profile_btn);
		
		actionbar.setCustomView(actionbarCustomLayout,new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,
				ActionBar.LayoutParams.MATCH_PARENT));
		
		actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
		
		mainMenu = new SlidingMenu(this);
		mainMenu.setMode(SlidingMenu.RIGHT);
		mainMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
		mainMenu.setShadowWidthRes(R.dimen.shadow_width);
        mainMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		mainMenu.setFadeDegree(0.35f);
		mainMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
		mainMenu.setMenu(R.layout.slidemenu_main);
		
		
		//sliding menu click
		profile_btn.setOnClickListener(profile_click);
		
		addBtn = (Button)findViewById(R.id.add_btn);
		addBtn.setOnClickListener(addGroup_click);
		
		//groupadd_dialog
		numOfGroup = 0;
		
		
		//pager marker
		pageMark = (LinearLayout)findViewById(R.id.page_mark);
		mainPager = (ViewPager)findViewById(R.id.main_pager);
		
		prevPosition = 0;
		numOfGrid = 0;
		mainPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int position) 
			{
				pageMark.getChildAt(prevPosition).setBackgroundResource(R.drawable.main_gray);
				pageMark.getChildAt(position).setBackgroundResource(R.drawable.main_orange);
				
				prevPosition = position;
			}
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {}
			@Override
			public void onPageScrollStateChanged(int arg0) {}
			
		});

		//page adapter
		//page adapter add순서 -> addGrid()->addItem()->addPageMarker();
		
		mainAdapter = new MainPagerAdapter(getApplicationContext());
		mainPager.setAdapter(mainAdapter);
		
				
	}
	
	private void addPageMarker()
	{
		ImageView iv = new ImageView(getApplicationContext());
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(30,30);
		params.rightMargin=10;
		iv.setLayoutParams(params);
		iv.setScaleType(ScaleType.FIT_XY);
		
		if(numOfGrid == 0)
		{
			iv.setBackgroundResource(R.drawable.main_orange);
			numOfGrid++;
		}
		else 
			iv.setBackgroundResource(R.drawable.main_gray);
		pageMark.addView(iv);
	}

	private OnClickListener profile_click = new OnClickListener() 
	{
		public void onClick(View v) 
		{
			if(isclicked == false)
			{
				mainMenu.showMenu();
				isclicked = true;
			}
			else
			{
				mainMenu.showContent();
				isclicked= false;
			}
				
				
		}
	};
	
	private OnClickListener addGroup_click = new OnClickListener() 
	{
		@Override
		public void onClick(View v) 
		{
			groupAddDialog = new GroupAddDialog(MainActivity.this,addGroupBtn_click);
			groupAddDialog.show();
			
			
		}
	};
	
	private OnClickListener addGroupBtn_click = new OnClickListener()
	{	
		@Override
		public void onClick(View v) 
		{
			groupAddDialog.dismiss();
			numOfGroup++;
			
			if(numOfGroup == 1)
			{
				mainAdapter.addGrid();
				addPageMarker();
			}
	
			mainAdapter.addItem(R.drawable.ic_launcher,groupAddDialog.getGroupName());
			
			if(numOfGroup%12 == 0)
			{
				//make grid
				mainAdapter.addGrid();
				addPageMarker();
			}
		}
	};
	
	
	
}
